package cn.iocoder.aics.module.member.controller.admin.chatcontext;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.aics.framework.common.pojo.PageParam;
import cn.iocoder.aics.framework.common.pojo.PageResult;
import cn.iocoder.aics.framework.common.pojo.CommonResult;
import cn.iocoder.aics.framework.common.util.object.BeanUtils;
import static cn.iocoder.aics.framework.common.pojo.CommonResult.success;

import cn.iocoder.aics.framework.excel.core.util.ExcelUtils;

import cn.iocoder.aics.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.aics.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.aics.module.member.controller.admin.chatcontext.vo.*;
import cn.iocoder.aics.module.member.dal.dataobject.chatcontext.ChatContextDO;
import cn.iocoder.aics.module.member.service.chatcontext.ChatContextService;

@Tag(name = "管理后台 - 聊天上下文")
@RestController
@RequestMapping("/member/chat-context")
@Validated
public class ChatContextController {

    @Resource
    private ChatContextService chatContextService;

    @PostMapping("/create")
    @Operation(summary = "创建聊天上下文")
    @PreAuthorize("@ss.hasPermission('member:chat-context:create')")
    public CommonResult<Long> createChatContext(@Valid @RequestBody ChatContextSaveReqVO createReqVO) {
        return success(chatContextService.createChatContext(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新聊天上下文")
    @PreAuthorize("@ss.hasPermission('member:chat-context:update')")
    public CommonResult<Boolean> updateChatContext(@Valid @RequestBody ChatContextSaveReqVO updateReqVO) {
        chatContextService.updateChatContext(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除聊天上下文")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:chat-context:delete')")
    public CommonResult<Boolean> deleteChatContext(@RequestParam("id") Long id) {
        chatContextService.deleteChatContext(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得聊天上下文")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:chat-context:query')")
    public CommonResult<ChatContextRespVO> getChatContext(@RequestParam("id") Long id) {
        ChatContextDO chatContext = chatContextService.getChatContext(id);
        return success(BeanUtils.toBean(chatContext, ChatContextRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得聊天上下文分页")
    @PreAuthorize("@ss.hasPermission('member:chat-context:query')")
    public CommonResult<PageResult<ChatContextRespVO>> getChatContextPage(@Valid ChatContextPageReqVO pageReqVO) {
        PageResult<ChatContextDO> pageResult = chatContextService.getChatContextPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatContextRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出聊天上下文 Excel")
    @PreAuthorize("@ss.hasPermission('member:chat-context:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatContextExcel(@Valid ChatContextPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatContextDO> list = chatContextService.getChatContextPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "聊天上下文.xls", "数据", ChatContextRespVO.class,
                        BeanUtils.toBean(list, ChatContextRespVO.class));
    }

}